home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / program / 319 / compsrc1 / expmed.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-20  |  45.7 KB  |  1,420 lines

  1. /* Medium-level subroutines: convert bit-field store and extract
  2.    and shifts, multiplies and divides to rtl instructions.
  3.    Copyright (C) 1987, 1988 Free Software Foundation, Inc.
  4.  
  5. This file is part of GNU CC.
  6.  
  7. GNU CC is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY.  No author or distributor
  9. accepts responsibility to anyone for the consequences of using it
  10. or for whether it serves any particular purpose or works at all,
  11. unless he says so in writing.  Refer to the GNU CC General Public
  12. License for full details.
  13.  
  14. Everyone is granted permission to copy, modify and redistribute
  15. GNU CC, but only under the conditions described in the
  16. GNU CC General Public License.   A copy of this license is
  17. supposed to have been given to you along with GNU CC so you
  18. can know your rights and responsibilities.  It should be in a
  19. file named COPYING.  Among other things, the copyright notice
  20. and this notice must be preserved on all copies.  */
  21.  
  22.  
  23. #include "config.h"
  24. #include "rtl.h"
  25. #include "tree.h"
  26. #include "flags.h"
  27. #include "insn-flags.h"
  28. #include "insn-codes.h"
  29. #include "insn-config.h"
  30. #include "expr.h"
  31. #include "recog.h"
  32.  
  33. static rtx extract_split_bit_field ();
  34. static rtx extract_fixed_bit_field ();
  35. static void store_split_bit_field ();
  36. static void store_fixed_bit_field ();
  37.  
  38. /* Return an rtx representing minus the value of X.  */
  39.  
  40. rtx
  41. negate_rtx (x)
  42.      rtx x;
  43. {
  44.   if (GET_CODE (x) == CONST_INT)
  45.     return gen_rtx (CONST_INT, VOIDmode, - INTVAL (x));
  46.   else
  47.     return expand_unop (GET_MODE (x), neg_optab, x, 0, 0);
  48. }
  49.  
  50. /* Generate code to store value from rtx VALUE
  51.    into a bit-field within structure STR_RTX
  52.    containing BITSIZE bits starting at bit BITNUM.
  53.    FIELDMODE is the machine-mode of the FIELD_DECL node for this field.  */
  54.  
  55. /* ??? This should really have the ability to copy a word into a register
  56.    in order to store the bit-field into it, on machines whose insv insns
  57.    work that way.  */
  58.  
  59. rtx
  60. store_bit_field (str_rtx, bitsize, bitnum, fieldmode, value)
  61.      rtx str_rtx;
  62.      register int bitsize;
  63.      int bitnum;
  64.      enum machine_mode fieldmode;
  65.      rtx value;
  66. {
  67.   int unit = (GET_CODE (str_rtx) == MEM) ? BITS_PER_UNIT : BITS_PER_WORD;
  68.   register int offset = bitnum / unit;
  69.   register int bitpos = bitnum % unit;
  70.   register rtx op0 = str_rtx;
  71.   rtx value1;
  72.  
  73.   if (GET_CODE (op0) == SUBREG)
  74.     {
  75.       offset += SUBREG_WORD (op0);
  76.       op0 = SUBREG_REG (op0);
  77.     }
  78.  
  79.   value = protect_from_queue (value, 0);
  80.  
  81.   if (flag_force_mem)
  82.     value = force_not_mem (value);
  83.  
  84.   if (GET_MODE_SIZE (fieldmode) >= UNITS_PER_WORD)
  85.     {
  86.       /* Storing in a full-word or multi-word field in a register
  87.      can be done with just SUBREG.  */
  88.       if (GET_MODE (op0) != fieldmode)
  89.     op0 = gen_rtx (SUBREG, fieldmode, op0, offset);
  90.       emit_move_insn (op0, value);
  91.       return value;
  92.     }
  93.  
  94. #ifdef BYTES_BIG_ENDIAN
  95.   /* If OP0 is a register, BITPOS must count within a word.
  96.      But as we have it, it counts within whatever size OP0 now has.
  97.      On a bigendian machine, these are not the same, so convert.  */
  98.   if (GET_CODE (op0) != MEM && unit > GET_MODE_BITSIZE (GET_MODE (op0)))
  99.     bitpos += unit - GET_MODE_BITSIZE (GET_MODE (op0));
  100. #endif
  101.  
  102.   /* Storing an lsb-aligned field in a register
  103.      can be done with a movestrict instruction.  */
  104.  
  105.   if (GET_CODE (op0) != MEM
  106. #ifdef BYTES_BIG_ENDIAN
  107.       && bitpos + bitsize == unit
  108. #else
  109.       && bitpos == 0
  110. #endif
  111.       && (GET_MODE (op0) == fieldmode
  112.       || (movstrict_optab->handlers[(int) fieldmode].insn_code
  113.           != CODE_FOR_nothing)))
  114.     {
  115.       /* Get appropriate low part of the value being stored.  */
  116.       if (GET_CODE (value) == CONST_INT || GET_CODE (value) == REG)
  117.     value = gen_lowpart (fieldmode, value);
  118.       else if (!(GET_CODE (value) == SYMBOL_REF
  119.          || GET_CODE (value) == LABEL_REF
  120.          || GET_CODE (value) == CONST))
  121.     value = convert_to_mode (fieldmode, value, 0);
  122.  
  123.       if (GET_MODE (op0) == fieldmode)
  124.     emit_move_insn (op0, value);
  125.       else
  126.     emit_insn (GEN_FCN (movstrict_optab->handlers[(int) fieldmode].insn_code)
  127.            (gen_rtx (SUBREG, fieldmode, op0, offset), value));
  128.  
  129.       return value;
  130.     }
  131.  
  132.   /* From here on we can assume that the field to be stored in is an integer,
  133.      since it is shorter than a word.  */
  134.  
  135.   /* OFFSET is the number of words or bytes (UNIT says which)
  136.      from STR_RTX to the first word or byte containing part of the field.  */
  137.  
  138.   if (GET_CODE (op0) == REG)
  139.     {
  140.       if (offset != 0
  141.       || GET_MODE_SIZE (GET_MODE (op0)) > GET_MODE_SIZE (SImode))
  142.     op0 = gen_rtx (SUBREG, SImode, op0, offset);
  143.       offset = 0;
  144.     }
  145.   else
  146.     {
  147.       op0 = protect_from_queue (op0, 1);
  148.     }
  149.  
  150.   /* Now OFFSET is nonzero only if OP0 is memory
  151.      and is therefore always measured in bytes.  */
  152.  
  153. #ifdef HAVE_insv
  154.   if (HAVE_insv
  155.       && !(bitsize == 1 && GET_CODE (value) == CONST_INT))
  156.     {
  157.       enum machine_mode mode0 = GET_MODE (op0);
  158.  
  159.       /* Add OFFSET into OP0's address.  */
  160.       if (GET_CODE (op0) == MEM)
  161.     op0 = change_address (op0, QImode,
  162.                   plus_constant (XEXP (op0, 0), offset));
  163.  
  164.       /* If op0 is a register, we need it in SImode
  165.      to make it acceptable to the format of insv.  */
  166.       if (GET_CODE (op0) == SUBREG)
  167.     PUT_MODE (op0, SImode);
  168.       if (GET_CODE (op0) == REG && GET_MODE (op0) != SImode)
  169.     op0 = gen_rtx (SUBREG, SImode, op0, 0);
  170.  
  171.       /* Convert VALUE to SImode (which insv insn wants) in VALUE1.  */
  172.       value1 = value;
  173.       if (GET_MODE (value) != SImode)
  174.     {
  175.       if (GET_MODE_BITSIZE (GET_MODE (value)) >= bitsize)
  176.         {
  177.           if (GET_CODE (value) != REG)
  178.         value1 = copy_to_reg (value);
  179.           /* Optimization: Don't bother really extending VALUE
  180.          if it has all the bits we will actually use.  */
  181.           value1 = gen_rtx (SUBREG, SImode, value1, 0);
  182.         }
  183.       else if (!CONSTANT_P (value))
  184.         /* Parse phase is supposed to make VALUE's data type
  185.            match that of the component reference, which is a type
  186.            at least as wide as the field; so VALUE should have
  187.            a mode that corresponds to that type.  */
  188.         abort ();
  189.     }
  190.  
  191.       /* If this machine's insv insists on a register,
  192.      get VALUE1 into a register.  */
  193.       if (! (*insn_operand_predicate[(int) CODE_FOR_insv][3]) (value1, SImode))
  194.     value1 = force_reg (SImode, value1);
  195.  
  196.       /* On big-endian machines, we count bits from the most significant.
  197.      If the bit field insn does not, we must invert.  */
  198.  
  199. #if defined (BITS_BIG_ENDIAN) != defined (BYTES_BIG_ENDIAN)
  200.       bitpos = unit - 1 - bitpos;
  201. #endif
  202.  
  203.       emit_insn (gen_insv (op0,
  204.                gen_rtx (CONST_INT, VOIDmode, bitsize),
  205.                gen_rtx (CONST_INT, VOIDmode, bitpos),
  206.                value1));
  207.     }
  208.   else
  209. #endif
  210.     /* Insv is not available; store using shifts and boolean ops.  */
  211.     store_fixed_bit_field (op0, offset, bitsize, bitpos, value);
  212.   return value;
  213. }
  214.  
  215. /* Use shifts and boolean operations to store VALUE
  216.    into a bit field of width BITSIZE
  217.    in a memory location specified by OP0 except offset by OFFSET bytes.
  218.    The field starts at position BITPOS within the byte.
  219.     (If OP0 is a register, it may be SImode or a narrower mode,
  220.      but BITPOS still counts within a full word,
  221.      which is significant on bigendian machines.)
  222.  
  223.    Note that protect_from_queue has already been done on OP0 and VALUE.  */
  224.  
  225. static void
  226. store_fixed_bit_field (op0, offset, bitsize, bitpos, value)
  227.      register rtx op0;
  228.      register int offset, bitsize, bitpos;
  229.      register rtx value;
  230. {
  231.   register enum machine_mode mode;
  232.   int total_bits = BITS_PER_WORD;
  233.   rtx subtarget;
  234.   int all_zero = 0;
  235.   int all_one = 0;
  236.  
  237.   /* Add OFFSET to OP0's address (if it is in memory)
  238.      and if a single byte contains the whole bit field
  239.      change OP0 to a byte.  */
  240.  
  241.   if (GET_CODE (op0) == REG || GET_CODE (op0) == SUBREG)
  242.     {
  243.       /* Special treatment for a bit field split across two registers.  */
  244.       if (bitsize + bitpos > BITS_PER_WORD)
  245.     {
  246.       store_split_bit_field (op0, bitsize, bitpos, value);
  247.       return;
  248.     }
  249.     }
  250.   else if (bitsize + bitpos <= BITS_PER_UNIT
  251.        && ! SLOW_BYTE_ACCESS)
  252.     {
  253.       total_bits = BITS_PER_UNIT;
  254.       op0 = change_address (op0, QImode, 
  255.                 plus_constant (XEXP (op0, 0), offset));
  256.     }
  257.   else
  258.     {
  259.       /* Get ref to word containing the field.  */
  260.       /* Adjust BITPOS to be position within a word,
  261.      and OFFSET to be the offset of that word.
  262.      Then alter OP0 to refer to that word.  */
  263.       bitpos += (offset % (BITS_PER_WORD / BITS_PER_UNIT)) * BITS_PER_UNIT;
  264.       offset -= (offset % (BITS_PER_WORD / BITS_PER_UNIT));
  265.       op0 = change_address (op0, SImode,
  266.                 plus_constant (XEXP (op0, 0), offset));
  267.       /* Special treatment for a bit field split across two words.  */
  268.       if (bitsize + bitpos > BITS_PER_WORD)
  269.     {
  270.       store_split_bit_field (op0, bitsize, bitpos, value);
  271.       return;
  272.     }
  273.     }
  274.  
  275.   mode = GET_MODE (op0);
  276.  
  277.   /* Now OP0 is either a byte or a word, and the bit field is contained
  278.      entirely within it.  TOTAL_BITS and MODE say which one (byte or word).
  279.      BITPOS is the starting bit number within the byte or word.
  280.      (If OP0 is a word, it may actually have a mode narrower than SImode.)  */
  281.  
  282. #ifdef BYTES_BIG_ENDIAN
  283.   /* BITPOS is the distance between our msb
  284.      and that of the containing byte or word.
  285.      Convert it to the distance from the lsb.  */
  286.  
  287.   bitpos = total_bits - bitsize - bitpos;
  288. #endif
  289.   /* Now BITPOS is always the distance between our lsb
  290.      and that of the containing byte or word.  */
  291.  
  292.   /* Shift VALUE left by BITPOS bits.  If VALUE is not constant,
  293.      we must first convert its mode to MODE.  */
  294.  
  295.   if (GET_CODE (value) == CONST_INT)
  296.     {
  297.       register int v = INTVAL (value);
  298.  
  299.       if (bitsize < HOST_BITS_PER_INT)
  300.     v &= (1 << bitsize) - 1;
  301.  
  302.       if (v == 0)
  303.     all_zero = 1;
  304.       else if (bitsize < HOST_BITS_PER_INT && v == (1 << bitsize) - 1)
  305.     all_one = 1;
  306.  
  307.       value = gen_rtx (CONST_INT, VOIDmode, v << bitpos);
  308.     }
  309.   else
  310.     {
  311.       int must_and = (GET_MODE_BITSIZE (GET_MODE (value)) != bitsize);
  312.  
  313.       if (GET_MODE (value) != mode)
  314.     {
  315.       if (GET_CODE (value) == REG && mode == QImode)
  316.         value = gen_rtx (SUBREG, mode, value, 0);
  317.       else
  318.         value = convert_to_mode (mode, value, 1);
  319.     }
  320.  
  321.       if (must_and && bitsize < HOST_BITS_PER_INT)
  322.     value = expand_bit_and (mode, value,
  323.                 gen_rtx (CONST_INT, VOIDmode,
  324.                      (1 << bitsize) - 1),
  325.                 0);
  326.       if (bitpos > 0)
  327.     value = expand_shift (LSHIFT_EXPR, mode, value,
  328.                   build_int_2 (bitpos, 0), 0, 1);
  329.     }
  330.  
  331.   /* Now clear the chosen bits in OP0,
  332.      except that if VALUE is -1 we need not bother.  */
  333.  
  334.   subtarget = op0;
  335.  
  336.   if (! all_one)
  337.     subtarget = expand_bit_and (mode, op0,
  338.                 gen_rtx (CONST_INT, VOIDmode, 
  339.                      (~ (((1 << bitsize) - 1) << bitpos))
  340.                      & ((GET_MODE_BITSIZE (mode)
  341.                          == HOST_BITS_PER_INT)
  342.                         ? -1
  343.                         : ((1 << GET_MODE_BITSIZE (mode)) - 1))),
  344.                 subtarget);
  345.  
  346.   /* Now logical-or VALUE into OP0, unless it is zero.  */
  347.  
  348.   if (! all_zero)
  349.     subtarget = expand_binop (mode, ior_optab, subtarget, value,
  350.                   op0, 1, OPTAB_LIB_WIDEN);
  351.   if (op0 != subtarget)
  352.     emit_move_insn (op0, subtarget);
  353. }
  354.  
  355. /* Store a bit field that is split across two words.
  356.  
  357.    OP0 is the REG, SUBREG or MEM rtx for the first of the two words.
  358.    BITSIZE is the field width; BITPOS the position of its first bit
  359.    (within the word).
  360.    VALUE is the value to store.  */
  361.  
  362. static void
  363. store_split_bit_field (op0, bitsize, bitpos, value)
  364.      rtx op0;
  365.      int bitsize, bitpos;
  366.      rtx value;
  367. {
  368.   /* BITSIZE_1 is size of the part in the first word.  */
  369.   int bitsize_1 = BITS_PER_WORD - bitpos;
  370.   /* BITSIZE_2 is size of the rest (in the following word).  */
  371.   int bitsize_2 = bitsize - bitsize_1;
  372.   rtx part1, part2;
  373.  
  374.   if (GET_MODE (value) != VOIDmode)
  375.     value = convert_to_mode (SImode, value, 1);
  376.   if (CONSTANT_P (value) && GET_CODE (value) != CONST_INT)
  377.     value = copy_to_reg (value);
  378.  
  379.   /* Split the value into two parts:
  380.      PART1 gets that which goes in the first word; PART2 the other.  */
  381. #ifdef BYTES_BIG_ENDIAN
  382.   /* PART1 gets the more significant part.  */
  383.   if (GET_CODE (value) == CONST_INT)
  384.     {
  385.       part1 = gen_rtx (CONST_INT, VOIDmode,
  386.                (unsigned) (INTVAL (value)) >> bitsize_2);
  387.       part2 = gen_rtx (CONST_INT, VOIDmode,
  388.                (unsigned) (INTVAL (value)) & ((1 << bitsize_2) - 1));
  389.     }
  390.   else
  391.     {
  392.       part1 = extract_fixed_bit_field (SImode, value, 0, bitsize_1,
  393.                        BITS_PER_WORD - bitsize, 0, 1);
  394.       part2 = extract_fixed_bit_field (SImode, value, 0, bitsize_2,
  395.                        BITS_PER_WORD - bitsize_2, 0, 1);
  396.     }
  397. #else
  398.   /* PART1 gets the less significant part.  */
  399.   if (GET_CODE (value) == CONST_INT)
  400.     {
  401.       part1 = gen_rtx (CONST_INT, VOIDmode,
  402.                (unsigned) (INTVAL (value)) & ((1 << bitsize_1) - 1));
  403.       part2 = gen_rtx (CONST_INT, VOIDmode,
  404.                (unsigned) (INTVAL (value)) >> bitsize_1);
  405.     }
  406.   else
  407.     {
  408.       part1 = extract_fixed_bit_field (SImode, value, 0, bitsize_1, 0, 0, 1);
  409.       part2 = extract_fixed_bit_field (SImode, value, 0, bitsize_2,
  410.                        bitsize_1, 0, 1);
  411.     }
  412. #endif
  413.  
  414.   /* Store PART1 into the first word.  */
  415.   store_fixed_bit_field (op0, 0, bitsize_1, bitpos, part1);
  416.  
  417.   /* Offset op0 to get to the following word.  */
  418.   if (GET_CODE (op0) == MEM)
  419.     op0 = change_address (op0, SImode,
  420.               plus_constant (XEXP (op0, 0), UNITS_PER_WORD));
  421.   else if (GET_CODE (op0) == REG)
  422.     op0 = gen_rtx (SUBREG, SImode, op0, 1);
  423.   else
  424.     op0 = gen_rtx (SUBREG, SImode, SUBREG_REG (op0), SUBREG_WORD (op0) + 1);
  425.  
  426.   /* Store PART2 into the second word.  */
  427.   store_fixed_bit_field (op0, 0, bitsize_2, 0, part2);
  428. }
  429.  
  430. /* Generate code to extract a byte-field from STR_RTX
  431.    containing BITSIZE bits, starting at BITNUM,
  432.    and put it in TARGET if possible (if TARGET is nonzero).
  433.    Regardless of TARGET, we return the rtx for where the value is placed.
  434.    It may be a QUEUED.
  435.  
  436.    STR_RTX is the structure containing the byte (a REG or MEM).
  437.    UNSIGNEDP is nonzero if this is an unsigned bit field.
  438.    MODE is the natural mode of the field value once extracted.
  439.    TMODE is the mode the caller would like the value to have;
  440.    but the value may be returned with type MODE instead.
  441.  
  442.    If a TARGET is specified and we can store in it at no extra cost,
  443.    we do so, and return TARGET.
  444.    Otherwise, we return a REG of mode TMODE or MODE, with TMODE preferred
  445.    if they are equally easy.  */
  446.  
  447. rtx
  448. extract_bit_field (str_rtx, bitsize, bitnum, unsignedp, target, mode, tmode)
  449.      rtx str_rtx;
  450.      register int bitsize;
  451.      int bitnum;
  452.      int unsignedp;
  453.      rtx target;
  454.      enum machine_mode mode, tmode;
  455. {
  456.   int unit = (GET_CODE (str_rtx) == MEM) ? BITS_PER_UNIT : BITS_PER_WORD;
  457.   register int offset = bitnum / unit;
  458.   register int bitpos = bitnum % unit;
  459.   register rtx op0 = str_rtx;
  460.   rtx spec_target = target;
  461.   rtx bitsize_rtx, bitpos_rtx;
  462.   rtx spec_target_subreg = 0;
  463.  
  464.   if (tmode == VOIDmode)
  465.     tmode = mode;
  466.  
  467.   while (GET_CODE (op0) == SUBREG)
  468.     {
  469.       offset += SUBREG_WORD (op0);
  470.       op0 = SUBREG_REG (op0);
  471.     }
  472.   
  473. #ifdef BYTES_BIG_ENDIAN
  474.   /* If OP0 is a register, BITPOS must count within a word.
  475.      But as we have it, it counts within whatever size OP0 now has.
  476.      On a bigendian machine, these are not the same, so convert.  */
  477.   if (GET_CODE (op0) != MEM && unit > GET_MODE_BITSIZE (GET_MODE (op0)))
  478.     bitpos += unit - GET_MODE_BITSIZE (GET_MODE (op0));
  479. #endif
  480.  
  481.   /* Extracting a full-word or multi-word value
  482.      from a structure in a register.
  483.      This can be done with just SUBREG.
  484.      So too extracting a subword value in
  485.      the least significant part of the register.  */
  486.  
  487.   if (GET_CODE (op0) == REG
  488.       && (bitsize >= BITS_PER_WORD
  489.       || ((bitsize == GET_MODE_BITSIZE (mode)
  490.            || bitsize == GET_MODE_BITSIZE (QImode)
  491.            || bitsize == GET_MODE_BITSIZE (HImode))
  492. #ifdef BYTES_BIG_ENDIAN
  493.           && bitpos + bitsize == BITS_PER_WORD
  494. #else
  495.           && bitpos == 0
  496. #endif
  497.           )))
  498.     {
  499.       enum machine_mode mode1 = mode;
  500.  
  501.       if (bitsize == GET_MODE_BITSIZE (QImode))
  502.     mode1 = QImode;
  503.       if (bitsize == GET_MODE_BITSIZE (HImode))
  504.     mode1 = HImode;
  505.  
  506.       if (mode1 != GET_MODE (op0))
  507.     op0 = gen_rtx (SUBREG, mode1, op0, offset);
  508.  
  509.       if (mode1 != mode)
  510.     return convert_to_mode (tmode, op0, unsignedp);
  511.       return op0;
  512.     }
  513.   
  514.   /* From here on we know the desired field is smaller than a word
  515.      so we can assume it is an integer.  So we can safely extract it as one
  516.      size of integer, if necessary, and then truncate or extend
  517.      to the size that is wanted.  */
  518.  
  519.   /* OFFSET is the number of words or bytes (UNIT says which)
  520.      from STR_RTX to the first word or byte containing part of the field.  */
  521.  
  522.   if (GET_CODE (op0) == REG)
  523.     {
  524.       if (offset != 0
  525.       || GET_MODE_SIZE (GET_MODE (op0)) > GET_MODE_SIZE (SImode))
  526.     op0 = gen_rtx (SUBREG, SImode, op0, offset);
  527.       offset = 0;
  528.     }
  529.   else
  530.     {
  531.       op0 = protect_from_queue (str_rtx, 1);
  532.     }
  533.  
  534.   /* Now OFFSET is nonzero only for memory operands.  */
  535.  
  536.   if (unsignedp)
  537.     {
  538. #ifdef HAVE_extzv
  539.       if (HAVE_extzv)
  540.     {
  541.       /* Get ref to first byte containing part of the field.  */
  542.       if (GET_CODE (op0) == MEM)
  543.         {
  544.           if (! ((*insn_operand_predicate[(int) CODE_FOR_extzv][1])
  545.              (op0, GET_MODE (op0))))
  546.         {
  547.           /* If memory isn't acceptable for this operand,
  548.              copy it to a register.  */
  549.           unit = BITS_PER_WORD;
  550.           offset = bitnum / unit;
  551.           bitpos = bitnum % unit;
  552.           op0 = change_address (op0, SImode,
  553.                     plus_constant (XEXP (op0, 0), offset));
  554.           op0 = force_reg (GET_MODE (op0), op0);
  555. #ifdef BITS_BIG_ENDIAN
  556.           if (unit > GET_MODE_BITSIZE (GET_MODE (op0)))
  557.             bitpos += unit - GET_MODE_BITSIZE (GET_MODE (op0));
  558. #endif
  559.         }
  560.           else
  561.         op0 = change_address (op0, QImode,
  562.                       plus_constant (XEXP (op0, 0), offset));
  563.         }
  564.  
  565.       /* If op0 is a register, we need it in SImode
  566.          to make it acceptable to the format of extv.  */
  567.       if (GET_CODE (op0) == SUBREG)
  568.         PUT_MODE (op0, SImode);
  569.       if (GET_CODE (op0) == REG && GET_MODE (op0) != SImode)
  570.         op0 = gen_rtx (SUBREG, SImode, op0, 0);
  571.  
  572.       if (target == 0
  573.           || (flag_force_mem && GET_CODE (target) == MEM))
  574.         target = spec_target = gen_reg_rtx (tmode);
  575.  
  576.       if (GET_MODE (target) != SImode)
  577.         {
  578.           if (GET_CODE (target) == REG)
  579.         spec_target_subreg = target = gen_rtx (SUBREG, SImode, target, 0);
  580.           else
  581.         target = gen_reg_rtx (SImode);
  582.         }
  583.  
  584.       /* If this machine's extzv insists on a register target,
  585.          make sure we have one.  */
  586.       if (! (*insn_operand_predicate[(int) CODE_FOR_extzv][0]) (target, SImode))
  587.         target = gen_reg_rtx (SImode);
  588.  
  589.       /* On big-endian machines, we count bits from the most significant.
  590.          If the bit field insn does not, we must invert.  */
  591. #if defined (BITS_BIG_ENDIAN) != defined (BYTES_BIG_ENDIAN)
  592.       bitpos = unit - 1 - bitpos;
  593. #endif
  594.  
  595.       bitsize_rtx = gen_rtx (CONST_INT, VOIDmode, bitsize);
  596.       bitpos_rtx = gen_rtx (CONST_INT, VOIDmode, bitpos);
  597.  
  598.       emit_insn (gen_extzv (protect_from_queue (target, 1),
  599.                 op0, bitsize_rtx, bitpos_rtx));
  600.     }
  601.       else
  602. #endif
  603.     target = extract_fixed_bit_field (tmode, op0, offset, bitsize, bitpos,
  604.                       target, 1);
  605.     }
  606.   else
  607.     {
  608. #ifdef HAVE_extv
  609.       if (HAVE_extv)
  610.     {
  611.       /* Get ref to first byte containing part of the field.  */
  612.       if (GET_CODE (op0) == MEM)
  613.         {
  614.           if (! ((*insn_operand_predicate[(int) CODE_FOR_extzv][1])
  615.              (op0, GET_MODE (op0))))
  616.         {
  617.           /* If memory isn't acceptable for this operand,
  618.              copy it to a register.  */
  619.           unit = BITS_PER_WORD;
  620.           offset = bitnum / unit;
  621.           bitpos = bitnum % unit;
  622.           op0 = change_address (op0, SImode,
  623.                     plus_constant (XEXP (op0, 0), offset));
  624.           op0 = force_reg (GET_MODE (op0), op0);
  625. #ifdef BITS_BIG_ENDIAN
  626.           if (unit > GET_MODE_BITSIZE (GET_MODE (op0)))
  627.             bitpos += unit - GET_MODE_BITSIZE (GET_MODE (op0));
  628. #endif
  629.         }
  630.           else
  631.         op0 = change_address (op0, QImode,
  632.                       plus_constant (XEXP (op0, 0), offset));
  633.         }
  634.  
  635.  
  636.       /* If op0 is a register, we need it in QImode
  637.          to make it acceptable to the format of extv.  */
  638.       if (GET_CODE (op0) == SUBREG)
  639.         PUT_MODE (op0, SImode);
  640.       if (GET_CODE (op0) == REG && GET_MODE (op0) != SImode)
  641.         op0 = gen_rtx (SUBREG, SImode, op0, 0);
  642.  
  643.       if (target == 0
  644.           || (flag_force_mem && GET_CODE (target) == MEM))
  645.         target = spec_target = gen_reg_rtx (tmode);
  646.  
  647.       if (GET_MODE (target) != SImode)
  648.         {
  649.           if (GET_CODE (target) == REG)
  650.         spec_target_subreg = target = gen_rtx (SUBREG, SImode, target, 0);
  651.           else
  652.         target = gen_reg_rtx (SImode);
  653.         }
  654.  
  655.       /* If this machine's extv insists on a register target,
  656.          make sure we have one.  */
  657.       if (! (*insn_operand_predicate[(int) CODE_FOR_extzv][0]) (target, SImode))
  658.         target = gen_reg_rtx (SImode);
  659.  
  660.       /* On big-endian machines, we count bits from the most significant.
  661.          If the bit field insn does not, we must invert.  */
  662. #if defined (BITS_BIG_ENDIAN) != defined (BYTES_BIG_ENDIAN)
  663.       bitpos = unit - 1 - bitpos;
  664. #endif
  665.  
  666.       bitsize_rtx = gen_rtx (CONST_INT, VOIDmode, bitsize);
  667.       bitpos_rtx = gen_rtx (CONST_INT, VOIDmode, bitpos);
  668.  
  669.       emit_insn (gen_extv (protect_from_queue (target, 1), op0,
  670.                    bitsize_rtx, bitpos_rtx));
  671.     }
  672.       else
  673. #endif
  674.     target = extract_fixed_bit_field (tmode, op0, offset, bitsize, bitpos,
  675.                       target, 0);
  676.     }
  677.   if (target == spec_target)
  678.     return target;
  679.   if (target == spec_target_subreg)
  680.     return spec_target;
  681.   if (GET_MODE (target) != tmode && GET_MODE (target) != mode)
  682.     return convert_to_mode (tmode, target, unsignedp);
  683.   return target;
  684. }
  685.  
  686. /* Extract a bit field using shifts and boolean operations
  687.    Returns an rtx to represent the value.
  688.    OP0 addresses a register (word) or memory (byte).
  689.    BITPOS says which bit within the word or byte the bit field starts in.
  690.    OFFSET says how many bytes farther the bit field starts;
  691.     it is 0 if OP0 is a register.
  692.    BITSIZE says how many bits long the bit field is.
  693.     (If OP0 is a register, it may be narrower than SImode,
  694.      but BITPOS still counts within a full word,
  695.      which is significant on bigendian machines.)
  696.  
  697.    UNSIGNEDP is nonzero for an unsigned bit field (don't sign-extend value).
  698.    If TARGET is nonzero, attempts to store the value there
  699.    and return TARGET, but this is not guaranteed.
  700.    If TARGET is not used, create a pseudo-reg of mode TMODE for the value.  */
  701.  
  702. static rtx
  703. extract_fixed_bit_field (tmode, op0, offset, bitsize, bitpos, target, unsignedp)
  704.      enum machine_mode tmode;
  705.      register rtx op0, target;
  706.      register int offset, bitsize, bitpos;
  707.      int unsignedp;
  708. {
  709.   int total_bits = BITS_PER_WORD;
  710.   enum machine_mode mode;
  711.   rtx orig = op0;
  712.  
  713.   if (GET_CODE (op0) == SUBREG || GET_CODE (op0) == REG)
  714.     {
  715.       /* Special treatment for a bit field split across two registers.  */
  716.       if (bitsize + bitpos > BITS_PER_WORD)
  717.     return extract_split_bit_field (op0, bitsize, bitpos, unsignedp);
  718.     }
  719.   else if (bitsize + bitpos <= BITS_PER_UNIT && ! SLOW_BYTE_ACCESS)
  720.     {
  721.       /* If the bit field fits entirely in one byte of memory,
  722.      let OP0 be that byte.  We must add OFFSET to its address.  */
  723.       total_bits = BITS_PER_UNIT;
  724.       op0 = change_address (op0, QImode,
  725.                 plus_constant (XEXP (op0, 0), offset));
  726.     }
  727.   else
  728.     {
  729. #ifdef STRICT_ALIGNMENT
  730. #ifndef STRUCTURE_SIZE_BOUNDARY
  731.       /* The following code assumes that OP0 is aligned
  732.      such that a word can be fetched there.
  733.      This could be because words don't need to be aligned,
  734.      or because all structures are suitably aligned.  */
  735.       abort ();
  736. #endif
  737. #endif
  738.  
  739.       /* Get ref to word containing the field.  */
  740.       /* Adjust BITPOS to be position within a word,
  741.      and OFFSET to be the offset of that word.  */
  742.       bitpos += (offset % (BITS_PER_WORD / BITS_PER_UNIT)) * BITS_PER_UNIT;
  743.       offset -= (offset % (BITS_PER_WORD / BITS_PER_UNIT));
  744.       op0 = change_address (op0, SImode,
  745.                 plus_constant (XEXP (op0, 0), offset));
  746.  
  747.       /* Special treatment for a bit field split across two words.  */
  748.       if (bitsize + bitpos > BITS_PER_WORD)
  749.     return extract_split_bit_field (op0, bitsize, bitpos, unsignedp);
  750.     }
  751.  
  752.   mode = GET_MODE (op0);
  753.  
  754. #ifdef BYTES_BIG_ENDIAN
  755.   /* BITPOS is the distance between our msb
  756.      and that of the containing byte or word.
  757.      Convert it to the distance from the lsb.  */
  758.  
  759.   bitpos = total_bits - bitsize - bitpos;
  760. #endif
  761.   /* Now BITPOS is always the distance between our lsb
  762.      and that of the containing byte or word.
  763.      We have reduced the big-endian case to the little-endian case.  */
  764.  
  765.   if (unsignedp)
  766.     {
  767.       if (bitpos)
  768.     {
  769.       /* If the field does not already start at the lsb,
  770.          shift it so it does.  */
  771.       tree amount = build_int_2 (bitpos, 0);
  772.       /* Maybe propagate the target for the shift.  */
  773.       /* Certainly do so if we will return the value of the shift.  */
  774.       rtx subtarget = (target != 0 && GET_CODE (target) == REG
  775.                && FUNCTION_VALUE_REGNO_P (REGNO (target))
  776.                ? target : 0);
  777.       if (tmode != mode) subtarget = 0;
  778.       op0 = expand_shift (RSHIFT_EXPR, mode, op0, amount, subtarget, 1);
  779.     }
  780.       /* Convert the value to the desired mode.  */
  781.       if (mode != tmode)
  782.     op0 = convert_to_mode (tmode, op0, 1);
  783.  
  784.       /* Unless the msb of the field used to be the msb when we shifted,
  785.      mask out the upper bits.  */
  786.  
  787.       if ((GET_MODE_BITSIZE (mode) != bitpos + bitsize
  788. #if 0
  789. #ifdef SLOW_ZERO_EXTEND
  790.        /* Always generate an `and' if
  791.           we just zero-extended op0 and SLOW_ZERO_EXTEND, since it
  792.           will combine fruitfully with the zero-extend. */
  793.        || tmode != mode
  794. #endif
  795. #endif
  796.        )
  797.       && bitsize < HOST_BITS_PER_INT)
  798.     return expand_bit_and (GET_MODE (op0), op0,
  799.                    gen_rtx (CONST_INT, VOIDmode, (1 << bitsize) - 1),
  800.                    target);
  801.       return op0;
  802.     }
  803.  
  804.   /* To extract a signed bit-field, first shift its msb to the msb of the word,
  805.      then arithmetic-shift its lsb to the lsb of the word.  */
  806.   op0 = force_reg (mode, op0);
  807.   if (mode != tmode)
  808.     target = 0;
  809.   if (GET_MODE_BITSIZE (QImode) < GET_MODE_BITSIZE (mode)
  810.       && GET_MODE_BITSIZE (QImode) >= bitsize + bitpos)
  811.     mode = QImode, op0 = convert_to_mode (QImode, op0, 0);
  812.   if (GET_MODE_BITSIZE (HImode) < GET_MODE_BITSIZE (mode)
  813.       && GET_MODE_BITSIZE (HImode) >= bitsize + bitpos)
  814.     mode = HImode, op0 = convert_to_mode (HImode, op0, 0);
  815.   if (GET_MODE_BITSIZE (mode) != (bitsize + bitpos))
  816.     {
  817.       tree amount = build_int_2 (GET_MODE_BITSIZE (mode) - (bitsize + bitpos), 0);
  818.       /* Maybe propagate the target for the shift.  */
  819.       /* Certainly do so if we will return the value of the shift.  */
  820.       rtx subtarget = (target != 0 && GET_CODE (target) == REG
  821.                && FUNCTION_VALUE_REGNO_P (REGNO (target))
  822.                ? target : 0);
  823.       op0 = expand_shift (LSHIFT_EXPR, mode, op0, amount, subtarget, 1);
  824.     }
  825.  
  826.   return expand_shift (RSHIFT_EXPR, mode, op0,
  827.                build_int_2 (GET_MODE_BITSIZE (mode) - bitsize, 0), 
  828.                target, 0);
  829. }
  830.  
  831. /* Extract a bit field that is split across two words
  832.    and return an RTX for the result.
  833.  
  834.    OP0 is the REG, SUBREG or MEM rtx for the first of the two words.
  835.    BITSIZE is the field width; BITPOS, position of its first bit, in the word.
  836.    UNSIGNEDP is 1 if should zero-extend the contents; else sign-extend.  */
  837.  
  838. static rtx
  839. extract_split_bit_field (op0, bitsize, bitpos, unsignedp)
  840.      rtx op0;
  841.      int bitsize, bitpos, unsignedp;
  842. {
  843.   /* BITSIZE_1 is size of the part in the first word.  */
  844.   int bitsize_1 = BITS_PER_WORD - bitpos;
  845.   /* BITSIZE_2 is size of the rest (in the following word).  */
  846.   int bitsize_2 = bitsize - bitsize_1;
  847.   rtx part1, part2, result;
  848.  
  849.   /* Get the part of the bit field from the first word.  */
  850.   part1 = extract_fixed_bit_field (SImode, op0, 0, bitsize_1, bitpos, 0, 1);
  851.  
  852.   /* Offset op0 by 1 word to get to the following one.  */
  853.   if (GET_CODE (op0) == MEM)
  854.     op0 = change_address (op0, SImode,
  855.               plus_constant (XEXP (op0, 0), UNITS_PER_WORD));
  856.   else if (GET_CODE (op0) == REG)
  857.     op0 = gen_rtx (SUBREG, SImode, op0, 1);
  858.   else
  859.     op0 = gen_rtx (SUBREG, SImode, SUBREG_REG (op0), SUBREG_WORD (op0) + 1);
  860.  
  861.   /* Get the part of the bit field from the second word.  */
  862.   part2 = extract_fixed_bit_field (SImode, op0, 0, bitsize_2, 0, 0, 1);
  863.  
  864.   /* Shift the more significant part up to fit above the other part.  */
  865. #ifdef BYTES_BIG_ENDIAN
  866.   part1 = expand_shift (LSHIFT_EXPR, SImode, part1,
  867.             build_int_2 (bitsize_2, 0), 0, 1);
  868. #else
  869.   part2 = expand_shift (LSHIFT_EXPR, SImode, part2,
  870.             build_int_2 (bitsize_1, 0), 0, 1);
  871. #endif
  872.  
  873.   /* Combine the two parts with bitwise or.  This works
  874.      because we extracted both parts as unsigned bit fields.  */
  875.   result = expand_binop (SImode, ior_optab, part1, part2, 0, 1,
  876.              OPTAB_LIB_WIDEN);
  877.  
  878.   /* Unsigned bit field: we are done.  */
  879.   if (unsignedp)
  880.     return result;
  881.   /* Signed bit field: sign-extend with two arithmetic shifts.  */
  882.   result = expand_shift (LSHIFT_EXPR, SImode, result,
  883.              build_int_2 (BITS_PER_WORD - bitsize, 0), 0, 0);
  884.   return expand_shift (RSHIFT_EXPR, SImode, result,
  885.                build_int_2 (BITS_PER_WORD - bitsize, 0), 0, 0);
  886. }
  887.  
  888. /* Add INC into TARGET.  */
  889.  
  890. void
  891. expand_inc (target, inc)
  892.      rtx target, inc;
  893. {
  894.   rtx value = expand_binop (GET_MODE (target), add_optab,
  895.                 target, inc,
  896.                 target, 0, OPTAB_LIB_WIDEN);
  897.   if (value != target)
  898.     emit_move_insn (target, value);
  899. }
  900.  
  901. /* Subtract INC from TARGET.  */
  902.  
  903. void
  904. expand_dec (target, dec)
  905.      rtx target, dec;
  906. {
  907.   rtx value = expand_binop (GET_MODE (target), sub_optab,
  908.                 target, dec,
  909.                 target, 0, OPTAB_LIB_WIDEN);
  910.   if (value != target)
  911.     emit_move_insn (target, value);
  912. }
  913.  
  914. /* Output a shift instruction for expression code CODE,
  915.    with SHIFTED being the rtx for the value to shift,
  916.    and AMOUNT the tree for the amount to shift by.
  917.    Store the result in the rtx TARGET, if that is convenient.
  918.    If UNSIGNEDP is nonzero, do a logical shift; otherwise, arithmetic.
  919.    Return the rtx for where the value is.  */
  920.  
  921. /* Pastel, for shifts, converts shift count to SImode here
  922.    independent of the mode being shifted.
  923.    Should that be done in an earlier pass?
  924.    It turns out not to matter for C.  */
  925.  
  926. rtx
  927. expand_shift (code, mode, shifted, amount, target, unsignedp)
  928.      enum tree_code code;
  929.      register enum machine_mode mode;
  930.      rtx shifted;
  931.      tree amount;
  932.      register rtx target;
  933.      int unsignedp;
  934. {
  935.   register rtx op1, temp = 0;
  936.   register int left = (code == LSHIFT_EXPR || code == LROTATE_EXPR);
  937.   int try;
  938.   rtx negated = 0;
  939.   int rotate = code == LROTATE_EXPR || code == RROTATE_EXPR;
  940.  
  941.   /* Previously detected shift-counts computed by NEGATE_EXPR
  942.      and shifted in the other direction; but that does not work
  943.      on all machines.  */
  944.  
  945.   op1 = expand_expr (amount, 0, VOIDmode, 0);
  946.  
  947.   for (try = 0; temp == 0 && try < 3; try++)
  948.     {
  949.       enum optab_methods methods;
  950.       if (try == 0)
  951.     methods = OPTAB_DIRECT;
  952.       else if (try == 1)
  953.     methods = OPTAB_WIDEN;
  954.       else
  955.     methods = OPTAB_LIB_WIDEN;
  956.  
  957.       if (rotate)
  958.     {
  959.       /* Widening does not work for rotation.  */
  960.       if (methods != OPTAB_DIRECT)
  961.         methods = OPTAB_LIB;
  962.  
  963.       temp = expand_binop (mode,
  964.                    left ? rotl_optab : rotr_optab,
  965.                    shifted, op1, target, -1, methods);
  966.       /* If there is no shift instruction for the desired direction,
  967.          try negating the shift count and shifting in the other direction.
  968.          If a machine has only a left shift instruction then we are
  969.          entitled to assume it shifts right with negative args.  */
  970.       if (temp == 0)
  971.         {
  972.           if (negated != 0)
  973.         ;
  974.           else if (GET_CODE (op1) == CONST_INT)
  975.         negated = gen_rtx (CONST_INT, VOIDmode, -INTVAL (op1));
  976.           else
  977.         negated = expand_unop (mode, neg_optab, op1, 0, 0);
  978.           temp = expand_binop (mode,
  979.                    left ? rotr_optab : rotl_optab,
  980.                    shifted, negated, target, -1, methods);
  981.         }
  982.     }
  983.       else if (unsignedp)
  984.     {
  985.       temp = expand_binop (mode,
  986.                    left ? lshl_optab : lshr_optab,
  987.                    shifted, op1, target, unsignedp, methods);
  988.       if (temp == 0 && left)
  989.         temp = expand_binop (mode, ashl_optab,
  990.                  shifted, op1, target, unsignedp, methods);
  991.       if (temp == 0)
  992.         {
  993.           if (negated != 0)
  994.         ;
  995.           else if (GET_CODE (op1) == CONST_INT)
  996.         negated = gen_rtx (CONST_INT, VOIDmode, -INTVAL (op1));
  997.           else
  998.         negated = expand_unop (mode, neg_optab, op1, 0, 0);
  999.           temp = expand_binop (mode,
  1000.                    left ? lshr_optab : lshl_optab,
  1001.                    shifted, negated,
  1002.                    target, unsignedp, methods);
  1003.         }
  1004.  
  1005.       if (temp != 0)
  1006.         return temp;
  1007.     }
  1008.       /* Do arithmetic shifts.
  1009.      Also, if we are going to widen the operand, we can just as well
  1010.      use an arithmetic right-shift instead of a logical one.  */
  1011.       if (! rotate && (! unsignedp || (! left && methods == OPTAB_WIDEN)))
  1012.     {
  1013.       /* Arithmetic shift */
  1014.  
  1015.       temp = expand_binop (mode,
  1016.                    left ? ashl_optab : ashr_optab,
  1017.                    shifted, op1, target, unsignedp, methods);
  1018.       if (temp == 0)
  1019.         {
  1020.           if (negated != 0)
  1021.         ;
  1022.           else if (GET_CODE (op1) == CONST_INT)
  1023.         negated = gen_rtx (CONST_INT, VOIDmode, -INTVAL (op1));
  1024.           else
  1025.         negated = expand_unop (mode, neg_optab, op1, 0, 0);
  1026.           temp = expand_binop (mode,
  1027.                    left ? ashr_optab : ashl_optab,
  1028.                    shifted, negated, target, unsignedp, methods);
  1029.         }
  1030.       if (temp != 0)
  1031.         return temp;
  1032.     }
  1033.  
  1034.       if (unsignedp)
  1035.     {
  1036.       /* No logical shift insn in either direction =>
  1037.          try a bit-field extract instruction if we have one.  */
  1038. #ifdef HAVE_extzv
  1039. #ifndef BITS_BIG_ENDIAN
  1040.       if (HAVE_extzv && !left
  1041.           && ((methods == OPTAB_DIRECT && mode == SImode)
  1042.           || (methods == OPTAB_WIDEN
  1043.               && GET_MODE_SIZE (mode) < GET_MODE_SIZE (SImode))))
  1044.         {
  1045.           rtx shifted1 = convert_to_mode (SImode, shifted, 1);
  1046.           rtx target1 = target;
  1047.  
  1048.           /* If -fforce-mem, don't let the operand be in memory.  */
  1049.           if (flag_force_mem && GET_CODE (shifted1) == MEM)
  1050.         shifted1 = force_not_mem (shifted1);
  1051.  
  1052.           /* If this machine's extzv insists on a register for
  1053.          operand 1, arrange for that.  */
  1054.           if (! ((*insn_operand_predicate[(int) CODE_FOR_extzv][1])
  1055.              (shifted1, SImode)))
  1056.         shifted1 = force_reg (SImode, shifted1);
  1057.  
  1058.           /* If we don't have or cannot use a suggested target,
  1059.          make a place for the result, in the proper mode.  */
  1060.           if (methods == OPTAB_WIDEN || target1 == 0
  1061.           || ! ((*insn_operand_predicate[(int) CODE_FOR_extzv][0])
  1062.             (target1, SImode)))
  1063.         target1 = gen_reg_rtx (SImode);
  1064.  
  1065.           /* If this machine's extzv insists on a register for
  1066.          operand 3, arrange for that.  */
  1067.           if (! ((*insn_operand_predicate[(int) CODE_FOR_extzv][3])
  1068.              (op1, SImode)))
  1069.         op1 = force_reg (SImode, op1);
  1070.  
  1071.           op1 = protect_from_queue (op1, 1);
  1072.  
  1073.           /* TEMP gets the width of the bit field to extract:
  1074.          wordsize minus # bits to shift by.  */
  1075.           if (GET_CODE (op1) == CONST_INT)
  1076.         temp = gen_rtx (CONST_INT, VOIDmode,
  1077.                 (GET_MODE_BITSIZE (mode) - INTVAL (op1)));
  1078.           else
  1079.         temp = expand_binop (SImode, sub_optab,
  1080.                      gen_rtx (CONST_INT, VOIDmode,
  1081.                           GET_MODE_BITSIZE (mode)),
  1082.                      op1, gen_reg_rtx (SImode),
  1083.                      0, OPTAB_LIB_WIDEN);
  1084.           /* Now extract with width TEMP, omitting OP1 least sig bits.  */
  1085.           emit_insn (gen_extzv (protect_from_queue (target1, 1),
  1086.                     protect_from_queue (shifted1, 0),
  1087.                     temp, op1));
  1088.           return convert_to_mode (mode, target1, 1);
  1089.         }
  1090.       /* Can also do logical shift with signed bit-field extract
  1091.          followed by inserting the bit-field at a different position.
  1092.          That strategy is not yet implemented.  */
  1093. #endif /* not BITS_BIG_ENDIAN */
  1094. #endif /* HAVE_extzv */
  1095.       /* We have failed to generate the logical shift and will abort.  */
  1096.     }
  1097.     }
  1098.   if (temp == 0)
  1099.     abort ();
  1100.   return temp;
  1101. }
  1102.  
  1103. /* Output an instruction or two to bitwise-and OP0 with OP1
  1104.    in mode MODE, with output to TARGET if convenient and TARGET is not zero.
  1105.    Returns where the result is.  */
  1106.  
  1107. rtx
  1108. expand_bit_and (mode, op0, op1, target)
  1109.      enum machine_mode mode;
  1110.      rtx op0, op1, target;
  1111. {
  1112.   register rtx temp;
  1113.  
  1114.   /* First try to open-code it directly.  */
  1115.   temp = expand_binop (mode, and_optab, op0, op1, target, 1, OPTAB_DIRECT);
  1116.   if (temp == 0)
  1117.     {
  1118.       rtx compl;
  1119.       /* If that fails, try to open code using a clear-bits insn.  */
  1120.       if (GET_CODE (op1) == CONST_INT
  1121.       && GET_MODE_BITSIZE (mode) < HOST_BITS_PER_INT)
  1122.     compl = gen_rtx (CONST_INT, VOIDmode,
  1123.              ((1 << GET_MODE_BITSIZE (mode)) - 1) & ~INTVAL (op1));
  1124.       else
  1125.     compl = expand_unop (mode, one_cmpl_optab, op1, 0, 1);
  1126.       temp = expand_binop (mode, andcb_optab, op0, compl, target,
  1127.                1, OPTAB_DIRECT);
  1128.     }
  1129.   if (temp == 0)
  1130.     /* If still no luck, try library call or wider modes.  */
  1131.     temp = expand_binop (mode, and_optab, op0, op1, target,
  1132.              1, OPTAB_LIB_WIDEN);
  1133.  
  1134.   if (temp == 0)
  1135.     abort ();
  1136.   return temp;
  1137. }
  1138.  
  1139. /* Perform a multiplication and return an rtx for the result.
  1140.    MODE is mode of value; OP0 and OP1 are what to multiply (rtx's);
  1141.    TARGET is a suggestion for where to store the result (an rtx).
  1142.  
  1143.    We check specially for a constant integer as OP1.
  1144.    If you want this check for OP0 as well, then before calling
  1145.    you should swap the two operands if OP0 would be constant.  */
  1146.  
  1147. rtx
  1148. expand_mult (mode, op0, op1, target, unsignedp)
  1149.      enum machine_mode mode;
  1150.      register rtx op0, op1, target;
  1151.      int unsignedp;
  1152. {
  1153.   /* Don't use the function value register as a target
  1154.      since we have to read it as well as write it,
  1155.      and function-inlining gets confused by this.  */
  1156.   if (target && REG_P (target) && FUNCTION_VALUE_REGNO_P (REGNO (target)))
  1157.     target = 0;
  1158.  
  1159.   if (GET_CODE (op1) == CONST_INT)
  1160.     {
  1161.       register int foo;
  1162.       int bar;
  1163.       int negate = INTVAL (op1) < 0;
  1164.       int absval = INTVAL (op1) * (negate ? -1 : 1);
  1165.  
  1166.       /* Is multiplier a power of 2, or minus that?  */
  1167.       foo = exact_log2 (absval);
  1168.       if (foo >= 0)
  1169.     {
  1170.       rtx tem =  expand_shift (LSHIFT_EXPR, mode, op0,
  1171.                    build_int_2 (foo, 0),
  1172.                    target, 0);
  1173.       return negate ? negate_rtx (tem) : tem;
  1174.     }
  1175.       /* Is multiplier a sum of two powers of 2, or minus that?  */
  1176.       bar = floor_log2 (absval);
  1177.       foo = exact_log2 (absval - (1 << bar));
  1178.       if (bar >= 0 && foo >= 0)
  1179.     {
  1180.       rtx pow1 = ((foo == 0) ? op0
  1181.               : expand_shift (LSHIFT_EXPR, mode, op0,
  1182.                       build_int_2 (foo, 0),
  1183.                       0, 0));
  1184.       rtx pow2 = expand_shift (LSHIFT_EXPR, mode, op0,
  1185.                    build_int_2 (bar, 0),
  1186.                    0, 0);
  1187.       rtx tem = force_operand (gen_rtx (PLUS, mode, pow1, pow2), target);
  1188.       return negate ? negate_rtx (tem) : tem;
  1189.     }
  1190.     }
  1191.   /* This used to use umul_optab if unsigned,
  1192.      but I think that for non-widening multiply there is no difference
  1193.      between signed and unsigned.  */
  1194.   op0 = expand_binop (mode, smul_optab,
  1195.               op0, op1, target, unsignedp, OPTAB_LIB_WIDEN);
  1196.   if (op0 == 0)
  1197.     abort ();
  1198.   return op0;
  1199. }
  1200.  
  1201. /* Emit the code to divide OP0 by OP1, putting the result in TARGET
  1202.    if that is convenient, and returning where the result is.
  1203.    You may request either the quotient or the remainder as the result;
  1204.    specify REM_FLAG nonzero to get the remainder.
  1205.  
  1206.    CODE is the expression code for which kind of division this is;
  1207.    it controls how rounding is done.  MODE is the machine mode to use.
  1208.    UNSIGNEDP nonzero means do unsigned division.  */
  1209.  
  1210. /* ??? For CEIL_MOD_EXPR, can compute incorrect remainder with ANDI
  1211.    and then correct it by or'ing in missing high bits
  1212.    if result of ANDI is nonzero.
  1213.    For ROUND_MOD_EXPR, can use ANDI and then sign-extend the result.
  1214.    This could optimize to a bfexts instruction.
  1215.    But C doesn't use these operations, so their optimizations are
  1216.    left for later.  */
  1217.  
  1218. rtx
  1219. expand_divmod (rem_flag, code, mode, op0, op1, target, unsignedp)
  1220.      int rem_flag;
  1221.      enum tree_code code;
  1222.      enum machine_mode mode;
  1223.      register rtx op0, op1, target;
  1224.      int unsignedp;
  1225. {
  1226.   register rtx label;
  1227.   register rtx temp;
  1228.   int log = -1;
  1229.   int can_clobber_op0;
  1230.   int mod_insn_no_good = 0;
  1231.   rtx adjusted_op0 = op0;
  1232.  
  1233.   /* Don't use the function value register as a target
  1234.      since we have to read it as well as write it,
  1235.      and function-inlining gets confused by this.  */
  1236.   if (target && REG_P (target) && FUNCTION_VALUE_REGNO_P (REGNO (target)))
  1237.     target = 0;
  1238.  
  1239.   if (target == 0)
  1240.     {
  1241.       target = gen_reg_rtx (mode);
  1242.     }
  1243.  
  1244.   /* Don't clobber an operand while doing a multi-step calculation.  */
  1245.   if ((rem_flag && rtx_equal_p (target, op0))
  1246.       || rtx_equal_p (target, op1))
  1247.     target = gen_reg_rtx (mode);
  1248.  
  1249.   can_clobber_op0 = (GET_CODE (op0) == REG && op0 == target);
  1250.  
  1251.   if (GET_CODE (op1) == CONST_INT)
  1252.     log = exact_log2 (INTVAL (op1));
  1253.  
  1254.   /* If log is >= 0, we are dividing by 2**log, and will do it by shifting,
  1255.      which is really floor-division.  Otherwise we will really do a divide,
  1256.      and we assume that is trunc-division.
  1257.  
  1258.      We must correct the dividend by adding or subtracting something
  1259.      based on the divisor, in order to do the kind of rounding specified
  1260.      by CODE.  The correction depends on what kind of rounding is actually
  1261.      available, and that depends on whether we will shift or divide.  */
  1262.  
  1263.   switch (code)
  1264.     {
  1265.     case TRUNC_MOD_EXPR:
  1266.     case TRUNC_DIV_EXPR:
  1267.       if (log >= 0 && ! unsignedp)
  1268.     {
  1269.       label = gen_label_rtx ();
  1270.       if (! can_clobber_op0)
  1271.         adjusted_op0 = copy_to_suggested_reg (adjusted_op0, target);
  1272.       emit_cmp_insn (adjusted_op0, const0_rtx, 0, 0);
  1273.       emit_jump_insn (gen_bge (label));
  1274.       expand_inc (adjusted_op0, plus_constant (op1, -1));
  1275.       emit_label (label);
  1276.       mod_insn_no_good = 1;
  1277.     }
  1278.       break;
  1279.  
  1280.     case FLOOR_DIV_EXPR:
  1281.     case FLOOR_MOD_EXPR:
  1282.       if (log < 0 && ! unsignedp)
  1283.     {
  1284.       label = gen_label_rtx ();
  1285.       if (! can_clobber_op0)
  1286.         adjusted_op0 = copy_to_suggested_reg (adjusted_op0, target);
  1287.       emit_cmp_insn (adjusted_op0, const0_rtx, 0, 0);
  1288.       emit_jump_insn (gen_bge (label));
  1289.       expand_dec (adjusted_op0, op1);
  1290.       expand_inc (adjusted_op0, const1_rtx);
  1291.       emit_label (label);
  1292.       mod_insn_no_good = 1;
  1293.     }
  1294.       break;
  1295.  
  1296.     case CEIL_DIV_EXPR:
  1297.     case CEIL_MOD_EXPR:
  1298.       if (! can_clobber_op0)
  1299.     adjusted_op0 = copy_to_suggested_reg (adjusted_op0, target);
  1300.       if (log < 0)
  1301.     {
  1302.       if (! unsignedp)
  1303.         {
  1304.           label = gen_label_rtx ();
  1305.           emit_cmp_insn (adjusted_op0, const0_rtx, 0, 0);
  1306.           emit_jump_insn (gen_ble (label));
  1307.         }
  1308.       expand_inc (adjusted_op0, op1);
  1309.       expand_dec (adjusted_op0, const1_rtx);
  1310.       if (! unsignedp)
  1311.         emit_label (label);
  1312.     }
  1313.       else
  1314.     {
  1315.       adjusted_op0 = expand_binop (GET_MODE (target), add_optab,
  1316.                        adjusted_op0, plus_constant (op1, -1),
  1317.                        0, 0, OPTAB_LIB_WIDEN);
  1318.     }
  1319.       mod_insn_no_good = 1;
  1320.       break;
  1321.  
  1322.     case ROUND_DIV_EXPR:
  1323.     case ROUND_MOD_EXPR:
  1324.       if (! can_clobber_op0)
  1325.     adjusted_op0 = copy_to_suggested_reg (adjusted_op0, target);
  1326.       if (log < 0)
  1327.     {
  1328.       op1 = expand_shift (RSHIFT_EXPR, mode, op1, integer_one_node, 0, 0);
  1329.       if (! unsignedp)
  1330.         {
  1331.           label = gen_label_rtx ();
  1332.           emit_cmp_insn (adjusted_op0, const0_rtx, 0, 0);
  1333.           emit_jump_insn (gen_bge (label));
  1334.           expand_unop (mode, neg_optab, op1, op1, 0);
  1335.           emit_label (label);
  1336.         }
  1337.       expand_inc (adjusted_op0, op1);
  1338.     }
  1339.       else
  1340.     {
  1341.       op1 = gen_rtx (CONST_INT, VOIDmode, INTVAL (op1) / 2);
  1342.       expand_inc (adjusted_op0, op1);
  1343.     }
  1344.       mod_insn_no_good = 1;
  1345.       break;
  1346.     }
  1347.  
  1348.   if (rem_flag && !mod_insn_no_good)
  1349.     {
  1350.       /* Try to produce the remainder directly */
  1351.       if (log >= 0)
  1352.     {
  1353.       return expand_bit_and (mode, adjusted_op0,
  1354.                  gen_rtx (CONST_INT, VOIDmode,
  1355.                       INTVAL (op1) - 1),
  1356.                  target);
  1357.     }
  1358.       else
  1359.     {
  1360.       /* See if we can do remainder without a library call.  */
  1361.       temp = expand_binop (mode,
  1362.                    unsignedp ? umod_optab : smod_optab,
  1363.                    adjusted_op0, op1, target,
  1364.                    unsignedp, OPTAB_WIDEN);
  1365.       if (temp != 0)
  1366.         return temp;
  1367.       /* No luck there.
  1368.          Can we do remainder and divide at once without a library call?  */
  1369.       temp = gen_reg_rtx (mode);
  1370.       if (expand_twoval_binop (unsignedp ? udivmod_optab : sdivmod_optab,
  1371.                    adjusted_op0, op1,
  1372.                    0, temp, unsignedp))
  1373.         return temp;
  1374.       temp = 0;
  1375.     }
  1376.     }
  1377.  
  1378.   /* Produce the quotient.  */
  1379.   if (log >= 0)
  1380.     temp = expand_shift (RSHIFT_EXPR, mode, adjusted_op0,
  1381.              build_int_2 (exact_log2 (INTVAL (op1)), 0),
  1382.              target, unsignedp);
  1383.   else if (rem_flag && !mod_insn_no_good)
  1384.     /* If producing quotient in order to subtract for remainder,
  1385.        and a remainder subroutine would be ok,
  1386.        don't use a divide subroutine.  */
  1387.     temp = expand_binop (mode, unsignedp ? udiv_optab : sdiv_optab,
  1388.              adjusted_op0, op1, target,
  1389.              unsignedp, OPTAB_WIDEN);
  1390.   else
  1391.     temp = expand_binop (mode, unsignedp ? udiv_optab : sdiv_optab,
  1392.              adjusted_op0, op1, target,
  1393.              unsignedp, OPTAB_LIB_WIDEN);
  1394.  
  1395.   /* If we really want the remainder, get it by subtraction.  */
  1396.   if (rem_flag)
  1397.     {
  1398.       if (temp == 0)
  1399.     {
  1400.       /* No divide instruction either.  Use library for remainder.  */
  1401.       temp = expand_binop (mode,
  1402.                    unsignedp ? umod_optab : smod_optab,
  1403.                    op0, op1, target,
  1404.                    unsignedp, OPTAB_LIB_WIDEN);
  1405.     }
  1406.       else
  1407.     {
  1408.       /* We divided.  Now finish doing X - Y * (X / Y).  */
  1409.       temp = expand_mult (mode, temp, op1, temp, unsignedp);
  1410.       if (! temp) abort ();
  1411.       temp = expand_binop (mode, sub_optab, op0,
  1412.                    temp, target, unsignedp, OPTAB_LIB_WIDEN);
  1413.     }
  1414.     }
  1415.  
  1416.   if (temp == 0)
  1417.     abort ();
  1418.   return temp;
  1419. }
  1420.